package com.yfp.operation.service.impl;

import com.yfp.common.core.enums.PackageStatusEnums;
import com.yfp.common.core.enums.PayStatusEnum;
import com.yfp.common.core.exception.ServiceException;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.operation.domain.MallProduct;
import com.yfp.operation.domain.OrderListDO;
import com.yfp.operation.enums.OrderStatus;
import com.yfp.operation.mapper.MallProductMapper;
import com.yfp.operation.service.MallProductService;
import com.yfp.operation.service.OrderComboService;
import com.yfp.operation.service.OrderDetailService;
import com.yfp.operation.service.OrderListService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 *
 * @CreateDate: 2025/2/24 15:10
 * @UpdateUser:
 * @UpdateDate: 2025/2/24 15:10
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@Service
public class MallProductServiceImpl implements MallProductService {

    @Resource
    private MallProductMapper mallProductMapper;
    @Resource
    private OrderListService orderListService;
    @Resource
    private OrderComboService orderComboService;
    @Resource
    private OrderDetailService orderDetailService;


    @Override
    public List<MallProduct> getList(MallProduct mallProduct) {
        return mallProductMapper.getList(mallProduct);
    }

    @Override
    public void add(MallProduct mallProduct) {
        mallProduct.setCurrentInventory(mallProduct.getInventory());
        mallProduct.setCityName("杭州市");
        mallProductMapper.add(mallProduct);
    }

    @Override
    @Transactional
    public void edit(MallProduct mallProduct) {
        MallProduct mallProduct1 = mallProductMapper.findInfoById(mallProduct.getId());
        int inventory = mallProduct.getInventory() - mallProduct1.getInventory();
        mallProduct.setCurrentInventory(mallProduct1.getCurrentInventory()+ inventory);
        mallProductMapper.edit(mallProduct);
    }

    @Override
    public MallProduct findInfoById(Long id) {
        return mallProductMapper.findInfoById(id);
    }

    @Override
    @Transactional
    public void verification(String orderSn) {
        List<OrderListDO> orderListDOS = orderListService.getByOrderSn(orderSn);
        for (OrderListDO orderListDO : orderListDOS) {
            if (!orderListDO.getPayStatus().equals(PayStatusEnum.PAID.getCode())){
                throw new ServiceException("订单不在待核销状态，请联系客服！！");
            }
        }
        orderListService.editPayStatusByOrderSn(PayStatusEnum.COMPLETED.getCode(), orderSn);
        orderComboService.editStatusByOrderId(orderSn, OrderStatus.Finish.getStatus());
        orderDetailService.editStatusByOrderId(orderSn, PackageStatusEnums.PickupIsComplete.getStatus());
    }
}
